package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {


        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        Integer totalOrderCount = reportMapper.ordersStatistics(map);
        map.put("status", Orders.COMPLETED);
        Double turnover = reportMapper.turnoverStatisticsMap(map);
        turnover = turnover == null? 0.0 : turnover;
        Integer validOrderCount = reportMapper.ordersStatistics(map);
        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        Integer newUsers = reportMapper.getIdCount(begin, end);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {

        Integer sold = workSpaceMapper.overviewSetmeals(StatusConstant.ENABLE);
        Integer discontinued = workSpaceMapper.overviewSetmeals(StatusConstant.DISABLE);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewDishes() {

        Integer sold = workSpaceMapper.overviewDishes(StatusConstant.ENABLE);
        Integer discontinued = workSpaceMapper.overviewDishes(StatusConstant.DISABLE);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
       //已取消
        Integer cancelledOrders = workSpaceMapper.overviewOrders(begin,end,Orders.CANCELLED);
        //全部
        Integer allOrders = workSpaceMapper.overviewOrders(begin, end, null);
        //已完成
        Integer completedOrders = workSpaceMapper.overviewOrders(begin,end,Orders.COMPLETED);
        //待派送
        Integer deliveredOrders = workSpaceMapper.overviewOrders(begin,end,Orders.CONFIRMED);
        //待接单
        Integer waitingOrders = workSpaceMapper.overviewOrders(begin,end,Orders.TO_BE_CONFIRMED);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
